package com.autumn.aitraffic.service.impl;


import com.autumn.aitraffic.api.Result;
import com.autumn.aitraffic.domain.excel.ExcelDriverHelp;
import com.autumn.aitraffic.domain.po.Driver;
import com.autumn.aitraffic.domain.vo.PageVo;
import com.autumn.aitraffic.mapper.DriverMapper;
import com.autumn.aitraffic.service.IDriverService;
import com.autumn.aitraffic.util.ExcelUtil;
import com.autumn.aitraffic.util.StringUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzx
 * @since 2025-01-22
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {

    @Override
    public Result pagequery(Page page, Driver driver) {
        Page<Driver> myPage = query().like(driver.getUsername() != null, "username", driver.getUsername())
                .like(driver.getTelephone() != null, "telephone", driver.getTelephone())
                .like(driver.getPlatenum() != null, "platenum", driver.getPlatenum())
                .select("id","username", "telephone", "platenum")
                .page(page);
        List<Driver> list = myPage.getRecords();
        PageVo pageVo = new PageVo();
        pageVo.setList(list);
        pageVo.setTotal(myPage.getTotal());
        return Result.succ("",pageVo);
    }

    @Override
    public Result updateDriver(Driver driver) {
        Driver  one = this.lambdaQuery().eq(Driver::getId, driver.getId())
                .one();
        if (one==null){
            return Result.fail("id错误用户不存在",null);
        }
        if (StringUtil.isNotBlank(driver.getTelephone())){
            one.setTelephone(driver.getTelephone());
        }
        if (StringUtil.isNotBlank(driver.getPlatenum())){
            one.setPlatenum(driver.getPlatenum());
        }
        if (StringUtil.isNotBlank(driver.getUsername())){
            one.setUsername(driver.getUsername());
        }
        this.updateById(one);
        return Result.succ(null,null);
    }

    @Override
    public Result importDriverData(MultipartFile file) {
        ExcelUtil<Driver> excelUtil = new ExcelUtil<Driver>();
        List<Driver> drivers = excelUtil.ExcelRead(file);
        //将list中数据转存到数据库 todo
        return Result.succ(null,null);
    }

    @Override
    public Result exportDriverData() {
        List<ExcelDriverHelp> excelWriteVos = new ArrayList<>();
        //从数据库导入数据 todo


        return null;
    }


}
